package com.adam.seatabusiness.business.service.impl;

import com.adam.seatabusiness.business.entity.Business;
import com.adam.seatabusiness.business.feign.OrderFeignService;
import com.adam.seatabusiness.business.feign.IntegralFeignService;
import com.adam.seatabusiness.business.mapper.BusinessMapper;
import com.adam.seatabusiness.business.service.BusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiDaXing
 * @since 2023-04-03
 */
@Slf4j
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private OrderFeignService orderFeignService;

    @Autowired
    private IntegralFeignService integralFeignService;

    @Override
    @GlobalTransactional(name = "seata-createBusiness",timeoutMills = 20000,rollbackFor = Exception.class)
    public void createBusiness(int money, String userId, String commodityCode, int point) {

        log.info("----->开始新建业务单");
        //1 新建业务单
        Business business = new Business();
        business.setMoney(money);
        business.setCommodityCode(commodityCode);
        business.setPoint(point);
        business.setStatus(0);
        business.setUserId(userId);
        businessMapper.createBusiness(business);

        //2 更新积分
        log.info("----->订单微服务开始更新积分，做增加start");
        integralFeignService.reduceIntegral(business.getPoint(), business.getCommodityCode());
        log.info("----->订单微服务开始更新积分，做增加end");

        //3 生成订单
        log.info("----->订单微服务开始调用订单，生成订单start");

        orderFeignService.createOrder(business.getUserId(), business.getMoney(),business.getCommodityCode());
        log.info("----->订单微服务开始调用订单，生成订单end");

        //4 修改订单状态，从零到1,1代表已经完成
        log.info("----->修改订单状态开始");
        businessMapper.finishBusiness();
        //模拟异常
        // int i=1/0;
        log.info("----->修改订单状态结束");

        log.info("----->下订单结束了！===============================================================");

    }
}
